from django.shortcuts import render
from rest_framework import mixins
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework import permissions
from .serializers import MovieInfoListSerializer, MovieInfoDetailSerializer,FilmScheduleSerializer
from user_movies_operations.models import UserWantMovie
from django.views.decorators.cache import cache_page
from rest_framework_extensions.cache.decorators import (
    cache_response
)
from .throttles import OncePerDayUserThrottle
from .filters import MovieInfoFilter
from rest_framework_extensions.cache.mixins import CacheResponseMixin
from django_filters import rest_framework as rest_filters
from rest_framework import filters

from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination


class LargeResultsSetPagination(PageNumberPagination):
    page_size = 1  # 每页默认显示几条
    page_size_query_param = 'page_size'  # 定制化显示几条
    max_page_size = 10  # 最大能显示几条每页


class MovieInfoViewSet(CacheResponseMixin, viewsets.ReadOnlyModelViewSet):
    """

    """
    serializer_class = MovieInfoListSerializer
    queryset = serializer_class.Meta.model.objects.all()
    filter_backends = (rest_filters.DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter)
    filter_class = MovieInfoFilter
    search_fields = ('name', 'movie_brief')
    ordering_fields = ('add_time', 'id')
    # pagination_class = LargeResultsSetPagination

    # filterset_fields = ('name', 'kind_category__id','movie_state__name')
    # throttle_classes = (OncePerDayUserThrottle,)
    # permission_classes = (permissions.IsAuthenticatedOrReadOnly)
    def get_serializer_class(self):
        if self.action == 'list':
            return MovieInfoListSerializer
        if self.action == 'retrieve':
            return MovieInfoDetailSerializer

    def get_throttles(self):

        return [i() for i in (OncePerDayUserThrottle,)]


class FilmScheduleViewSet(viewsets.ReadOnlyModelViewSet):
    """

    """
    serializer_class = FilmScheduleSerializer
    queryset = serializer_class.Meta.model.objects.all()


